/*
 * SMPP Encoder/Decoder
 * Copyright (C) 2006 redtaza@users.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
/// @file bind_receiver.cpp
/// @brief Declares a bind_receiver

#include "unregister.h"

/// @brief Default constructor.
/// @note Sets all parameters to their default values.
Smpp::UnRegisterReq::UnRegisterReq() :
    Request(CommandLength(MinLength), 
    CommandId(CommandId::UnRegister_req), 
    SequenceNumber::Min)
{
}


Smpp::UnRegisterReq::~UnRegisterReq()
{
}

/// @brief Encode the message into an octet array.
/// @return Pointer to the encoded message.
/// @note The Buffer length is the command_length.
const Smpp::Uint8*
Smpp::UnRegisterReq::encode()
{
	buff_.reset(Header::command_length());

	Request::encode(buff_); // insert the header first
	buff_ += Smpp::hton32(OriID.value());
	buff_ += Smpp::hton32(DstID.value());
	buff_ += Smpp::hton32(UnRegID.value());

	Header::encode_tlvs(buff_);

	return buff_.get();
}


/// @brief Decode the message from an octet array.
/// @param buff The octet array to decode.
/// @note The octet array maybe the data read from a socket.
void
Smpp::UnRegisterReq::decode(const Smpp::Uint8* buff)
{
	Request::decode(buff);

	Smpp::Uint32 len = Request::command_length();
	Smpp::Uint32 offset = 16;
	const char* err = "Invalid bind PDU length";
	if(len < offset)
		throw Error(err);
 
	const Smpp::Char* sptr = reinterpret_cast<const Smpp::Char*>(buff)+offset;
	Smpp::Char* ptr=const_cast<Smpp::Char*>(sptr);
		
	OriID.decode(ptr);
	DstID.decode(ptr);
	UnRegID.decode(ptr);

	Header::decode_tlvs((Smpp::Uint8*)ptr, len - offset - (ptr-sptr));
}

